using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.Service.IOCExtensions
{
	/// <summary>
	/// This is an extension class for Customer DTO objects and lists
	/// </summary>
	public static partial class CustomerExtension
	{
		#region RunSelect
		/// <summary>
		/// Select all objects from store.
		/// </summary>
		public static void RunSelect(this List<CustomerDTO> list)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			list.AddRange(proxy.RunSelect());
		}
		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a paged result set.
		/// </summary>
		public static PagedQueryResults<CustomerDTO> RunSelect(this List<CustomerDTO> list, int page, int pageSize, bool ascending, string sortColumn, string linq)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			PagedQueryResults<CustomerDTO> retval = proxy.RunSelect(page, pageSize, ascending, sortColumn, linq);
			if (retval.GridData != null)
				list.AddRange(retval.GridData);
			return retval;
		}

		#endregion

		#region RunSelect Paged

		/// <summary>
		/// Using the specified Where expression, execute a query against the database to return a result set.
		/// </summary>
		public static void RunSelect(this List<CustomerDTO> list, string linq)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			list.AddRange(proxy.RunSelect(linq));
		}

		#endregion

		#region Persist

		/// <summary>
		/// Persists this object to store.
		/// </summary>
		public static void Persist(this CustomerDTO customer)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			proxy.Persist(customer);
		}

		/// <summary>
		/// Persists this collection to store.
		/// </summary>
		public static void Persist(this List<CustomerDTO> customerList)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			proxy.Persist(customerList);
		}

		#endregion

		#region Customer SelectUsingPK

		/// <summary>
		/// Select a single object from this collection by its primary key.
		/// </summary>
		public static bool SelectByPrimaryKey(this CustomerDTO customer, string customerid)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			ICustomerService customerService = (ICustomerService)proxy;
			var item = customerService.SelectByPrimaryKey(customerid);
			if (item == null) return false;
			CopyObject(item, customer);
			return true;
		}

		#endregion

		#region Customer Select By Foreign Key
		/// <summary>
		/// Get the related 'CustomerCustomerDemo' DTO list
		/// </summary>
		public static CustomerCustomerDemoDTO[] GetCustomerCustomerDemoList(this CustomerDTO item)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			ICustomerService service = (ICustomerService)proxy;
			return service.GetCustomerCustomerDemoList(item).ToArray();
		}

		/// <summary>
		/// Get the related 'Order' DTO list
		/// </summary>
		public static OrderDTO[] GetOrderList(this CustomerDTO item)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			ICustomerService service = (ICustomerService)proxy;
			return service.GetOrderList(item).ToArray();
		}


		#endregion

		#region Delete

		/// <summary>
		/// Delete a single object.
		/// </summary>
		public static void Delete(this CustomerDTO customer)
		{
			var proxy = ProxyFactory<CustomerDTO>.GetProxy();
			ICustomerService customerService = (ICustomerService)proxy;
			customerService.Delete(customer);
		}

		#endregion

		/// <summary>
		/// Clones one DTO to another.
		/// </summary>
		internal static void CopyObject(CustomerDTO source, CustomerDTO destination)
		{
			destination.Region = source.Region;
			destination.PostalCode = source.PostalCode;
			destination.Phone = source.Phone;
			destination.Fax = source.Fax;
			destination.CustomerId = source.CustomerId;
			destination.Country = source.Country;
			destination.ContactTitle = source.ContactTitle;
			destination.ContactName = source.ContactName;
			destination.CompanyName = source.CompanyName;
			destination.City = source.City;
			destination.Address = source.Address;
		}

	}
}
